<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('网格配置列表')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="gray-bg">
<div class="ui-layout-west">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa fa-clone"></i> 行政区划
            </div>
            <div class="box-tools pull-right">
                <a type="button" class="btn btn-box-tool" href="#"><i></i></a>
                <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i
                        class="fa fa-chevron-up"></i></button>
                <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i
                        class="fa fa-chevron-down"></i></button>
                <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新网格"><i
                        class="fa fa-refresh"></i></button>
            </div>
            <div class="treeSearchInput" id="search">
                <p class="row">
                    <input type="text" class="empty" id="keyword" placeholder="关键字" maxlength="50">
                    <button class="btn" id="btn" onclick="$.tree.searchNode()"> 搜索</button>
                </p>
            </div>
        </div>
        <div class="ui-layout-content">
            <div id="tree" class="ztree"></div>
        </div>
    </div>
</div>

<div class="ui-layout-center">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-8 search-collapse">
                <form id="formId">
                    <input type="hidden" name="code" id="code"/>
                </form>
            </div>
            <div class="btn-group-sm" id="toolbar" role="group">
            </div>
            <div class="col-sm-8 select-table table-striped">
                <table id="bootstrap-tree-table"></table>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: layout-latest-js"/>
<th:block th:include="include :: ztree-js"/>
<script th:inline="javascript">
    $(function () {
        var panehHidden = false;
        if ($(this).width() < 769) {
            panehHidden = true;
        }
        $('body').layout({initClosed: panehHidden, west__size: 300});
        // 回到顶部绑定
        if ($.fn.toTop !== undefined) {
            var opt = {
                win: $('.ui-layout-center'),
                doc: $('.ui-layout-center')
            };
            $('#scroll-up').toTop(opt);
        }
        queryAddressList();
        queryGridTree();
    });

    var addFlag = [[${@permission.hasPermi('system:addr:add')}]];
    var editFlag = [[${@permission.hasPermi('system:addr:edit')}]];
    var prefix = ctx + "system/addr";

    function queryAddressList() {
        var options = {
            code: "code",
            parentCode: "upCode",
            expandColumn: "1",
            uniqueId: "code",
            url: prefix + "/list",
            createUrl: prefix + "/add/{id}",
            updateUrl: prefix + "/edit/{id}",
            exportUrl: prefix + "/export",
            modalName: "五级行政区划",
            columns: [{
                field: 'selectItem',
                radio: true
            },
                {
                    field: 'code',
                    title: '行政区划编码',
                    align: 'left'
                },
                {
                    field: 'name',
                    title: '行政区划名称',
                    align: 'left'
                },
                {
                    field: 'upCode',
                    title: '上级编码',
                    align: 'left'
                },
                {
                    field: 'pLevel',
                    title: '层级',
                    align: 'left'
                },
                {
                    title: '操作',
                    align: 'center',
                    align: 'left',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="edit(\'' + row.code + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-info  btn-xs ' + addFlag + '" href="javascript:void(0)" onclick="add(\'' + row.code + '\',\'' + row.pLevel + '\')"><i class="fa fa-plus"></i>新增</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.treeTable.init(options);
    }

    function queryGridTree() {
        var url = ctx + "system/addr/treeData";
        var options = {
            url: url,
            expandLevel: 2,
            onClick: zOnClick
        };
        $.tree.init(options);

        function zOnClick(event, treeId, treeNode) {
            var treeId = treeNode.id;
            $("#code").val(treeId);
            $.treeTable.search();
        }
    }

    $('#btnExpand').click(function () {
        $._tree.expandAll(true);
        $(this).hide();
        $('#btnCollapse').show();
    });

    $('#btnCollapse').click(function () {
        $._tree.expandAll(false);
        $(this).hide();
        $('#btnExpand').show();
    });

    $('#btnRefresh').click(function () {
        queryGridTree();
    });

    function add(code, pLevel) {
        if (pLevel == 5) {
            $.modal.alertWarning("最小单元不能增加下级！");
            return;
        }
        var url = prefix + '/add/' + code + "/" + pLevel;
        $.modal.open("新增行政区划", url, 600, 400);
    }

    function edit(code) {
        var url = prefix + '/edit/' + code;
        $.modal.open("修改行政区划", url, 600, 400);
    }
</script>
</body>
</html>